package day04.demo

import org.apache.spark.{SparkConf, SparkContext}

/**
 * @Author wsl
 * @Description 热门商品topN  点击次数、下单次数、支付次数
 */
object TopN_Hot_Case {
  def main(args: Array[String]): Unit = {
    val conf: SparkConf = new SparkConf().setAppName("rdd").setMaster("local[*]")
    val sc = new SparkContext(conf)

    sc.textFile("sparkcore/input/user_visit_action.txt")
      .map(
        line => {
          val datas: Array[String] = line.split("_")
          UserVisitAction(
            datas(0),
            datas(1),
            datas(2),
            datas(3),
            datas(4),
            datas(5),
            datas(6),
            datas(7),
            datas(8),
            datas(9),
            datas(10),
            datas(11),
            datas(12))
        }
      ).flatMap(
      action => {
        if (action.click_category_id != "-1") {
          //点击数据
          List(CategoryCountInfo(action.click_category_id, 1, 0, 0))
        } else if (action.order_category_ids != "null") {
          //下单数据
          val arr: Array[String] = action.order_category_ids.split(",")
          arr.map(CategoryCountInfo(_, 0, 1, 0))
        } else if (action.pay_category_ids != "null") {
          //支付数据
          val arr: Array[String] = action.pay_category_ids.split(",")
          arr.map(CategoryCountInfo(_, 0, 0, 1))
        } else {
          Nil
        }
      }
    )
      .groupBy(info=>info.categoryId)
      .mapValues(
        datas => {
          datas.reduce(
            (info1, info2) => {
              info1.clickCount += info2.clickCount
              info1.orderCount += info2.orderCount
              info1.payCount += info2.payCount
              info1
            }
          )
        }
      ).map(_._2)
      .sortBy(info => (info.clickCount,info.orderCount,info.payCount),false)
      .take(10)
      .foreach(println)


    sc.stop()

  }
}

//用户访问动作表
case class UserVisitAction(date: String, //用户点击行为的日期
                           user_id: String, //用户的ID
                           session_id: String, //Session的ID
                           page_id: String, //某个页面的ID
                           action_time: String, //动作的时间点
                           search_keyword: String, //用户搜索的关键词
                           click_category_id: String, //点击商品品类的ID
                           click_product_id: String, //点击商品的ID
                           order_category_ids: String, //一次订单中所有品类的ID集合
                           order_product_ids: String, //一次订单中所有商品的ID集合
                           pay_category_ids: String, //一次支付中所有品类的ID集合
                           pay_product_ids: String, //一次支付中所有商品的ID集合
                           city_id: String) //城市 id

// 输出结果表
case class CategoryCountInfo(categoryId: String, //品类id
                             var clickCount: Long, //点击次数
                             var orderCount: Long, //订单次数
                             var payCount: Long) //支付次数